import Vue from 'vue'
import Vuex from 'vuex'
import RoomStore from './RoomStore.js'
import log from './log.js'
import User from './User.js'


Vue.use(Vuex)

export default new Vuex.Store({
        state:{
            loadging:false,
            text:"loading",
            loadingTime:null,
            // 弹窗
            Tips:'',
            // 记录用户所处大厅or房间or多台
            type:1,
            ListStatusName:{},
            // 记录多台哪种桌子下注超时
            typeManyNumber:0,
            // 初始化个人内容
            Init:{
                UId:"",
                GT:1,//游戏类型
                GTableId:0,//台桌
                OnlineNum:0,//当前在线人数
                UserCentent:{//用户信息
                    Balance:0,//余额
                    uMemberXH:0,//个人限红
                    Video:""
                },
            },
            // 大厅
            Hall:{
                Notice:"别人的代码，我脑壳疼",//公告
                RoomArray:[],//房间列表
            },
            //房间
            Room:{
                voice:true,
                BetHistory:"0,0,0,0,0,0,0,0",
                BetMode:0,
                ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                CurrJu:0,
                CurrState:2,
                CurrXue:0,
                DjsTime:0,
                GType:1,
                G_Result:0,
                HallNumber:1,
                LuDan:"",
                MoneyMax:0,
                MoneyMin:0,
                PaiMian:"",
                PlayerNum:1,
                RestHops:0,
                RoomName:"",
                Room_attribute:1,
                TId:1,
                VideoUrl:["",""],
                WinLose:0,
            },
            //多台
            ManyGame:[
                {
                    BetHistory:"0,0,0,0,0,0,0,0",
                    BetMode:0,
                    ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                    CurrJu:0,
                    CurrState:0,
                    CurrXue:0,
                    DjsTime:null,
                    GType:0,
                    G_Result:"",
                    HallNumber:1,
                    LuDan:"",
                    MoneyMax:0,
                    MoneyMin:0,
                    PaiMian:"",
                    RestHops:null,
                    RoomName:"",
                    TId:0,
                    VideoUrl:"",
                    WinLose:0
                },
                {
                    BetHistory:"0,0,0,0,0,0,0,0",
                    BetMode:0,
                    ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                    CurrJu:0,
                    CurrState:0,
                    CurrXue:0,
                    DjsTime:null,
                    GType:0,
                    G_Result:"",
                    HallNumber:1,
                    LuDan:"",
                    MoneyMax:0,
                    MoneyMin:0,
                    PaiMian:"",
                    RestHops:null,
                    RoomName:"",
                    TId:0,
                    VideoUrl:"",
                    WinLose:0
                },
                {
                    BetHistory:"0,0,0,0,0,0,0,0",
                    BetMode:0,
                    ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                    CurrJu:0,
                    CurrState:0,
                    CurrXue:0,
                    DjsTime:null,
                    GType:0,
                    G_Result:"",
                    HallNumber:1,
                    LuDan:"",
                    MoneyMax:0,
                    MoneyMin:0,
                    PaiMian:"",
                    RestHops:null,
                    RoomName:"",
                    TId:0,
                    VideoUrl:"",
                    WinLose:0
                },
                {
                    BetHistory:"0,0,0,0,0,0,0,0",
                    BetMode:0,
                    ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                    CurrJu:0,
                    CurrState:0,
                    CurrXue:0,
                    DjsTime:null,
                    GType:0,
                    G_Result:"",
                    HallNumber:1,
                    LuDan:"",
                    MoneyMax:0,
                    MoneyMin:0,
                    PaiMian:"",
                    RestHops:null,
                    RoomName:"",
                    TId:0,
                    VideoUrl:"",
                    WinLose:0
                },
        ],
            // 下注是否超时
            BetStatus:false,
            // 公共通知区
            publicWarehouse:{
                // 房间下注信息通知
                RoomBetInfo:false,
                // 房间下注失败通知
                RoomLoserBet:false,
                ManyGame:[
                    {RoomBetInfo:false,RoomLoserBet:false},
                    {RoomBetInfo:false,RoomLoserBet:false},
                    {RoomBetInfo:false,RoomLoserBet:false},
                    {RoomBetInfo:false,RoomLoserBet:false}
                ]
            },
            // 视频播放加载屏障数据
            VideoStatus:[true,true,true,true,true,true]
        },
        getters:{
            loadging(state){
                return state.loadging;
            },
            // 提示
            Tips(state){
                return state.Tips;
            },
            // 用户信息
            Init(state){
                return state.Init;
            },
            // 用户个人限红
            XH(state){
                let a=state.Init.UserCentent.uMemberXH;
                if(a){
                    let b=[];
                    b=a.split(",");
                    for(let i=0;i<b.length;i++){
                        b[i]=b[i].split("-");
                    }
                    switch(b.length){
                        case 1:
                            b.push(b[0]);
                            b.push(b[0]);
                        break;
                        case 2:
                            b.push(b[1]);
                        break;
                    }
                    return b;				
                }
            },
            // 大厅信息
            Hall(state){
                return state.Hall;
            },
            // 加载中的字体
            Text(state){
                return state.text;
            },
            // 房间
            Room(state){
                return state.Room;
            },
            // 公告
            Hall_Notice(state){
                return state.Hall.Notice;
            },
            // 大厅房间信息
            Hall_RoomArray(state){
                return state.Hall.RoomArray;
            },
            // 公共通知区
            publicWarehouse(state){
                return state.publicWarehouse;
            },
            // 多台
            ManyGame(state){
                return state.ManyGame;
            },
            // 视频状态
            videoStatus(state){
                return state.VideoStatus;
            },
            ListStatusName(state){
                return state.ListStatusName;
            }
        },
        mutations:{
            // 修改订阅名
            chanegRadisnName(state,name){
                state.ListStatusName[name]=name;
            },
            // 清空订阅名
            clearRadisnName(state){
                state.ListStatusName={}
            },
            // 改变加载中状态
            changeLoading(state,x){
                // console.log('-----------');
                // console.log(window.TimeOut)
                // console.log(window.AJAXtime)
                // console.log(window.interrupt)
                // console.log('-----------');
                window.TimeOut?clearTimeout(window.TimeOut):false;
                window.AJAXtime?clearTimeout(window.AJAXtime):false;
                window.interrupt?clearTimeout(window.interrupt):false;
                if(x.id=="下注中"){
                        state.loadging=true;
                        state.text=x.id;
                        state.BetStatus=true;				
                        //房间中
                        if(state.type==2){
                            window.AJAXtime= setTimeout(()=>{//获取结果超时请求查看
                                    $.ajax({
                                        url:Api.betHos,
                                        data:{
                                            "UserId":UserId,
                                            "RandCode":RandCode,
                                            "GameType":state.Room.GType,
                                            "GTableId":state.Room.TId
                                        },
                                        success:(data)=>{
                                            if(data.State==1000){
                                                if(data.Msg.BetInfo){//存在下注
                                                    let Msg=data.Msg;
                                                    let a={Content:{Balance:Msg.Balance, BetInfo:Msg.BetInfo , CurrJu:Msg.Curr_Xue, CurrXue:Msg.Curr_Ju, BetHistory:Msg.BetHistory }};//模拟socket返回数据
                                                    this.commit("getBetInfo",a);
                                                    this.commit("changeLoading",false);
                                                    state.BetStatus=false;
                                                }else{//没有获取到下注
                                                    // 发送心跳包测活
                                                    window.userSocket.uSend('{"ActType":11,"ConnectionId":"","GT":"","UId":"' + UserId + '","ErrorCode":10000,"RandCode":"' + RandCode + '","UId":"' + UserId + '"}')
                                                }						
                                            }
                                        },
                                        error:()=>{//没有获取到下注
                                            // 发送心跳包测活
                                            window.userSocket.uSend('{"ActType":11,"ConnectionId":"","GT":"","UId":"' + UserId + '","ErrorCode":10000,"RandCode":"' + RandCode + '","UId":"' + UserId + '"}')
                                        }
    
                                    })
                                },5000)					
                        }
                        //多台中
                        else if(state.type==3){
                            //记录第几台下注了超时
                            for(let i=0;i<state.ManyGame.length;i++){
                                if(state.ManyGame[i].TId==x.GTableId){
                                    state.typeManyNumber=i;
                                }
                            }
                            window.AJAXtime= setTimeout(()=>{//获取结果超时请求查看
                                $.ajax({
                                    url:Api.betHos,
                                    data:{
                                        "UserId":UserId,
                                        "RandCode":RandCode,
                                        "GameType":x.GType,
                                        "GTableId":x.GTableId
                                    },
                                    success:(data)=>{
                                        if(data.State==1000){
                                            if(data.Msg.BetInfo){//存在下注
                                                let Msg=data.Msg;
                                                let a={Content:{Balance:Msg.Balance, BetInfo:Msg.BetInfo , CurrJu:Msg.Curr_Xue, CurrXue:Msg.Curr_Ju, BetHistory:Msg.BetHistory},GT:Msg.GameType,GTableId:Msg.GTableId};//模拟socket返回数据
                                                this.commit("getBetInfo",a);
                                                this.commit("changeLoading",false);
                                            }else{//没有获取到下注
                                                // 发送心跳包测活
                                                window.userSocket.uSend('{"ActType":11,"ConnectionId":"","GT":"","UId":"' + UserId + '","ErrorCode":10000,"RandCode":"' + RandCode + '","UId":"' + UserId + '"}')
                                            }						
                                        }
                                    },
                                    error:(data)=>{//没有获取到下注
                                        // 发送心跳包测活
                                        window.userSocket.uSend('{"ActType":11,"ConnectionId":"","GT":"","UId":"' + UserId + '","ErrorCode":10000,"RandCode":"' + RandCode + '","UId":"' + UserId + '"}')
                                    }
    
                                })
                            },5000)
                        }
    
                }
                else{
                    state.text="loading";
                    state.BetStatus=false;//下注监控状态
                    state.loadging=x;
                }				
            },
            // 改变提示弹窗
            changeTips(state,x){
                if(x.Out){
                    // let zh = sessionStorage.getItem("UserId")
                    sessionStorage.clear();
                    // window.userSocket.uSend(JSON.stringify({ActType:12,UId:UserId,RandCode}));
                    window.userSocket.allStop();
                    alert(x.data);				
                    window.location.href="./index.html";					
                    // window.location.href="./index.html?"+zh+"&123456";					
                }else{
                    state.Tips=x.data;
                }
            },
            // 倒计时秒数
            setTimes(state,x){
                if(state.Hall.RoomArray[x].RestHops>0){
                    state.Hall.RoomArray[x].RestHops--;
                }
            },
            // 修改列表房间开奖推牌等状态
            setRoomStatus(state,x){
                state.Hall.RoomArray[x.id].CurrState=x.data;
            },
            // 游戏登录函数
            setInit(state,Msg){
                // state.Init.GT=Msg.GT;
                state.Init.UId=Msg.UId;
                // state.Init.GTableId=Msg.GTableId;
                // state.Init.OnlineNum=Msg.OnlineNum;
                state.Init.UserCentent.Balance=Msg.Content.Balance;
                state.Init.UserCentent.uMemberXH=Msg.Content.MemberXH;
                let a=Msg.Content.Video.split("**");
                state.Init.UserCentent.Video=a[0];
            },
            // 进入游戏大厅处理函数
            goGameHall(state,Msg){
                if(state.type==2){
                    window.AudioObj.Play(["tabUi"])//播放切页音效
                }
                state.type!=1?state.type=1:false;//切位置为大厅
                state.Init.GT!=Msg.GT?state.Init.GT=Msg.GT:false;//切换游戏类型
                state.Init.OnlineNum!=Msg.OnlineNum?state.Init.OnlineNum=Msg.OnlineNum:false;//当前人数
                state.Init.UserCentent.Balance!=Msg.Content.Balance?state.Init.UserCentent.Balance=Msg.Content.Balance:false;//改变余额
                state.Hall.Notice!=Msg.Content.Notice?state.Hall.Notice=Msg.Content.Notice:false;//公告变动
                state.Hall.RoomArray!=Msg.Content.RoomArray?state.Hall.RoomArray=Msg.Content.RoomArray:false;//改变房间列表数据
            },
            // 初始化列表广播
            initTableRoom(state,index){
                state.Hall.RoomArray[index].ChipsSum="0/0,0/0,0/0,0/0,0/0";
            },
            // 处理列表广播
            changeTableRoom(state,Msg){
                let a=0;
                let index=0;
                // 获取对应变动的房间下标
                state.Hall.RoomArray.forEach(array=>{
                    if(array.TId==Msg.TId){
                        index=a;
                    }
                    a++;
                })
                // 进行数据写入
                let i;
                let dateRoom = state.Hall.RoomArray[index];
                for(i in Msg){
                    if(Msg[i]!==""){
                        if(dateRoom.hasOwnProperty(i)){
                            dateRoom[i]!=Msg[i]?dateRoom[i]=Msg[i]:false;
                        }else{
                            dateRoom[i]=Msg[i]
                        }
                        
                    }
                }
                
            },
            // 初始化房间的数据
            InitRoom(state){
                state.Room={
                    voice:true,
                    BetHistory:"0,0,0,0,0,0,0,0",
                    BetMode:0,
                    ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                    CurrJu:0,
                    CurrState:2,
                    CurrXue:0,
                    DjsTime:0,
                    GType:1,
                    G_Result:0,
                    HallNumber:1,
                    LuDan:"",
                    MoneyMax:0,
                    MoneyMin:0,
                    PaiMian:"",
                    PlayerNum:1,
                    RestHops:0,
                    RoomName:"",
                    Room_attribute:1,
                    TId:1,
                    VideoUrl:["",""],
                    WinLose:0,
                }
            },
            // 进入房间处理获取到的数据
            setRoomInit(state,Msg){
                state.VideoStatus[0]=true;
                state.VideoStatus[1]=true;
                this.commit("InitRoom");
                window.AudioObj.Play(["tabUi"])//播放切页面音效
                if(state.type!=2){
                    state.type=2
                    let j;
                    for(j in state.ListStatusName){
                        window.ioSocket.emit('unsubscribe',state.ListStatusName[j]);
                    }
                    state.ListStatusName={}
                    window.ioSocket.emit('subscribe','RoomListInRoom');
                    window.ioSocket.emit('subscribe','RoomBroadCastAfterTZ'+Msg.GTableId);
                }
                state.Init.GT!=Msg.GT?state.Init.GT=Msg.GT:false;
                state.Init.GTableId!=Msg.GTableId?state.Init.GTableId=Msg.GTableId:false;
                state.Room.BetHistory!=Msg.Content.BetHistory?state.Room.BetHistory=Msg.Content.BetHistory:false;
                let a;
                for(a in Msg.Content){
                    if(a=="Balance"){
                        continue;
                    }
                    else if(a=="WinLose"||a=="PaiMian"||a=="G_Result"){
                        state.Room[a]=null;
                    }
                    else if(a=="VideoUrl"){
                        state.Room[a] = Msg.Content[a].indexOf("**")!=-1?Msg.Content[a].split("**"):[Msg.Content[a]];
                    }
                    else{
                        state.Room[a]!=Msg.Content[a]?state.Room[a]=Msg.Content[a]:false;
                    }
                }
                if(state.Room.GType==1){
                    window.AudioObj.Play(["welcomeBJL"]);
                }else if(state.Room.GType==2){
                    window.AudioObj.Play(["welcomelh"]);
                }
            },
            // 房间倒计时减少函数
            reduceRoomTime(state){
                if(state.Room.RestHops>0){
                    state.Room.RestHops--;
                    if(state.Room.RestHops==11){//播放警告下注声音
                        window.AudioObj.Play(['countingdown']);
                    }else if(state.Room.RestHops==5){//停止下注
                        window.AudioObj.Play(['stopbet']);
                    }
                }
            },
            // 房间自定义下注减少增加总筹码(单机)
            reduceMoney(state,x){
                state.Init.UserCentent.Balance+=x;
            },
            // 下注成功返回处理
            getBetInfo(state,Msg){
                console.log('下注成功');
                console.log(Msg);
                state.Init.UserCentent.Balance=Msg.Content.Balance;//设置余额
                if(state.type==2){// 用户在房间下注
                    state.Room.CurrJu!=Msg.Content.CurrJu?state.Room.CurrJu=Msg.Content.CurrJu:false;//改变房间鞋局
                    state.Room.CurrXue!=Msg.Content.CurrXue?state.Room.CurrJu=Msg.Content.CurrXue:false;//改变房间局
                    state.publicWarehouse.RoomBetInfo=Msg.Content;//发布公共变动
                }else if(state.type==3){//用户在多台投注
                    for(var i=0;i<state.ManyGame.length;i++){
                        if(state.ManyGame[i].TId==Msg.GTableId&&state.ManyGame[i].GType==Msg.GT){
                            state.ManyGame[i].CurrJu!=Msg.Content.CurrJu?state.ManyGame[i].CurrJu=Msg.Content.CurrJu:false;
                            state.ManyGame[i].CurrXue!=Msg.Content.CurrXue?state.ManyGame[i].CurrXue=Msg.Content.CurrXue:false;
                            // 发布公共变动
                            state.publicWarehouse.ManyGame.splice(i,1,{RoomBetInfo:Msg.Content,RoomLoserBet:false})
                        }
                    }
                }
            },
            // 下注失败回调
            setBetInLoser(state,Msg){
                // 房间内
                if(state.type==2){
                    state.publicWarehouse.RoomLoserBet=true;				
                }
                // 多台
                else if(state.type==3){
                    for(var i=0;i<state.ManyGame.length;i++){
                        if(state.ManyGame[i].TId==Msg.GTableId&&state.ManyGame[i].GType==Msg.GT){
                            // 发布公共变动
                            state.publicWarehouse.ManyGame.splice(i,1,{RoomBetInfo:false,RoomLoserBet:true});
                        }
                    }
                }
            },
            // 检查下注返回是否超时
            BetTimeOut(state){
                if(state.BetStatus){
                    // 用户处于房间
                    if(state.type==2){
                        $.ajax({
                            url:Api.betHos,
                            data:{
                            "UserId":UserId,
                            "RandCode":RandCode,
                            "GameType":state.Room.GType,
                            "GTableId":state.Room.TId
                            },
                            success:(data)=>{
                                if(data.State==1000){
                                    let Msg=data.Msg;
                                    let a={Content:{Balance:Msg.Balance, BetInfo:Msg.BetInfo , CurrJu:Msg.Curr_Xue, CurrXue:Msg.Curr_Ju, BetHistory:Msg.BetHistory }};//模拟socket返回数据
                                    if(data.Msg.BetInfo){//存在下注
                                        this.commit("getBetInfo",a);
                                        this.commit("changeLoading",false);
                                        this.commit("changeTips",{data:"成功下注"});
                                    }else{//没有获取到下注
                                        this.commit("setBetInLoser")//下注失败回调
                                        this.commit("changeLoading",false);
                                        this.commit("changeTips",{data:"对不起，网络繁忙！下注失败！"});
                                    }						
                                }
                            },
                            error:()=>{//没有获取到下注
                                this.commit("setBetInLoser")//下注失败回调
                                this.commit("changeLoading",false);
                                this.commit("changeTips",{data:"对不起，网络繁忙！下注失败！"});
                                }
                        })
                        //恢复下注不超时
                        state.BetStatus=false;					
                    }
                    // 用户处于多台
                    else if(state.type==3){
                        //判断下注超时的台桌超时了没
                        if(state.ManyGame[state.typeManyNumber].RestHops==0){
                            $.ajax({
                                url:Api.betHos,
                                data:{
                                "UserId":UserId,
                                "RandCode":RandCode,
                                "GameType":state.ManyGame[state.typeManyNumber].GType,
                                "GTableId":state.ManyGame[state.typeManyNumber].TId
                                },
                                success:(data)=>{
                                    if(data.State==1000){
                                        let Msg=data.Msg;
                                        let a={Content:{Balance:Msg.Balance, BetInfo:Msg.BetInfo , CurrJu:Msg.Curr_Xue, CurrXue:Msg.Curr_Ju, BetHistory:Msg.BetHistory},GT:Msg.GameType,GTableId:Msg.GTableId};//模拟socket返回数据
                                        if(data.Msg.BetInfo){//存在下注
                                            this.commit("getBetInfo",a);
                                            this.commit("changeLoading",false);
                                            this.commit("changeTips",{data:"成功下注"});
                                        }else{//没有获取到下注
                                            this.commit("setBetInLoser",a)//下注失败回调
                                            this.commit("changeLoading",false);
                                            this.commit("changeTips",{data:"对不起，网络繁忙！下注失败！"});
                                        }						
                                    }
                                },
                                error:()=>{//没有获取到下注
                                    let a={GT:Msg.GameTypestate.ManyGame[state.typeManyNumber].GType, TId:state.ManyGame[state.typeManyNumber].TId};//模拟socket返回数据
                                    this.commit("setBetInLoser",a)//下注失败回调
                                    this.commit("changeLoading",false);
                                    this.commit("changeTips",{data:"对不起，网络繁忙！下注失败！"});
                                    }
                            })
                            //恢复下注不超时
                            state.BetStatus=false;						
                        }
                    }
                }			
            },
            // 初始化输赢结果，开牌结果
            InitRule(state,index){
                if(state.type==2){
                    state.Room.WinLose=0;
                    state.Room.G_Result="";
                    state.Room.PaiMian=""
                    state.Room.ChipsSum="0/0,0/0,0/0,0/0,0/0"				
                }
                else if(state.type==3){
                    state.ManyGame[index].WinLose=0;
                    state.ManyGame[index].G_Result="";
                    state.ManyGame[index].PaiMian=""
                    state.ManyGame[index].ChipsSum="0/0,0/0,0/0,0/0,0/0"	
                }
            },
            // 开奖处理
            openRule(state,Msg){
                // 判断在房间中还是在多台
                if(state.type==2){
                    // 在房间设置ROOM信息修改
                    if(state.Room.GType!=Msg.Content.GType||state.Room.TId!=Msg.GTableId){//检查推送信息是否有错
                        return false;
                    }
                    state.Room.OnlineNum!=Msg.OnlineNum?state.Room.OnlineNum=Msg.OnlineNum:false;
                    state.Init.UserCentent.Balance!=Msg.Content.Balance?state.Init.UserCentent.Balance=Msg.Content.Balance:false;
                    state.Room.WinLose=Msg.Content.WinLose;
                    state.Room.LuDan=Msg.Content.LuDan;
                    state.Room.G_Result=Msg.Content.G_Result
                    state.Room.CurrJu!=Msg.Content.CurrJu?state.Room.CurrJu=Msg.Content.CurrJu:false;
                    state.Room.CurrXue!=Msg.Content.CurrXue?state.Room.CurrXue=Msg.Content.CurrXue:false;
                    state.Room.PaiMian=Msg.Content.PaiMian;
                    state.Room.CurrState=5;
                    // 播放输赢音效
                    if(Msg.Content.WinLose>0){
                        window.AudioObj.Play(["win"]);
                    }else if(Msg.Content.WinLose<0){
                        window.AudioObj.Play(["loser"]);
                    }
                    setTimeout(()=>{//初始化结果
                        this.commit("InitRule");
                    },3800)
                }else if(state.type==3){
                    state.Init.UserCentent.Balance!=Msg.Content.Balance?state.Init.UserCentent.Balance=Msg.Content.Balance:false;
                    let ab;
                    for(let i=0;i<state.ManyGame.length;i++){
                        if(state.ManyGame[i].TId==Msg.GTableId&&state.ManyGame[i].GType==Msg.Content.GType){
                            ab=i;
                            state.ManyGame[i].WinLose=Msg.Content.WinLose;
                            state.ManyGame[i].LuDan=Msg.Content.LuDan;
                            state.ManyGame[i].G_Result=Msg.Content.G_Result
                            state.ManyGame[i].CurrJu!=Msg.Content.CurrJu?state.ManyGame[i].CurrJu=Msg.Content.CurrJu:false;
                            state.ManyGame[i].CurrXue!=Msg.Content.CurrXue?state.ManyGame[i].CurrXue=Msg.Content.CurrXue:false;
                            state.ManyGame[i].PaiMian=Msg.Content.PaiMian;
                            state.ManyGame[i].CurrState=5;
                        }
                    }
                    // 播放输赢音效
                    if(Msg.Content.WinLose>0){
                        window.AudioObj.Play(["jb"]);
                    }else if(Msg.Content.WinLose<0){
                        window.AudioObj.Play(["loser"]);
                    }
                    setTimeout(()=>{//初始化结果
                        this.commit("InitRule",ab);
                    },3800)
                }
            },
            //开局返回处理 and 多台处理
            openNewGame(state,Msg){
                if(state.type==2){//房间结果
                    if(state.Init.GT!=Msg.GT||state.Init.GTableId!=Msg.GTableId){
                        return false;
                    }
                    state.Room.CurrJu!=Msg.Content.CurrJu?state.Room.CurrJu=Msg.Content.CurrJu:false;
                    state.Room.CurrXue!=Msg.Content.CurrXue?state.Room.CurrXue=Msg.Content.CurrXue:false;
                    state.Room.DjsTime!=Msg.Content.DjsTime?state.Room.DjsTime=Msg.Content.DjsTime:false;
                    state.Room.RestHops!=Msg.Content.RestHops?state.Room.RestHops=Msg.Content.RestHops:false;
                    state.Room.OnlineNum!=Msg.OnlineNum?state.Room.OnlineNum=Msg.OnlineNum:false;
                    state.Room.PaiMian="";
                    state.Room.G_Result="";
                    state.Room.WinLose=0;
                    state.Room.CurrState=2;
                }
                else if(state.type==3){
                    for(let i=0;i<state.ManyGame.length;i++){
                        if(state.ManyGame[i].TId==Msg.GTableId){
                            state.ManyGame[i].CurrJu!=Msg.Content.CurrJu?state.ManyGame[i].CurrJu=Msg.Content.CurrJu:false;
                            state.ManyGame[i].CurrXue!=Msg.Content.CurrXue?state.ManyGame[i].CurrXue=Msg.Content.CurrXue:false;
                            state.ManyGame[i].DjsTime!=Msg.Content.DjsTime?state.ManyGame[i].DjsTime=Msg.Content.DjsTime:false;
                            state.ManyGame[i].RestHops!=Msg.Content.RestHops?state.ManyGame[i].RestHops=Msg.Content.RestHops:false;	
                            state.ManyGame[i].G_Result="";
                            state.ManyGame[i].PaiMian="";
                            state.ManyGame[i].WinLose=0;
                            state.ManyGame[i].CurrState=2;
                        }
                    }
                }
            },
            // 下注比例信息更新
            setUserAll(state,Msg){
                if(state.type==2){
                    if(state.Room.TId!=Msg.TId){
                        console.log("挂掉了")
                        return false;
                    }
                    // 进行数据写入
                    state.Room.ChipsSum!=Msg.ChipsSum?state.Room.ChipsSum=Msg.ChipsSum:false;
                    state.Room.OnlineNum!=Msg.OnlineNum?state.Room.OnlineNum=Msg.OnlineNum:false;
                }
                else if(state.type==3){
                    for(let i=0;i<state.ManyGame.length;i++){
                        if(state.ManyGame[i].TId==Msg.TId){
                            // 进行数据写入
                            state.ManyGame[i].ChipsSum!=Msg.ChipsSum?state.ManyGame[i].ChipsSum=Msg.ChipsSum:false;
                            state.ManyGame[i].OnlineNum!=Msg.OnlineNum?state.ManyGame[i].OnlineNum=Msg.OnlineNum:false;
                        }
                    }
                }
            },
            // 紧急停止倒计时处理
            setStop(state,Msg){
                if(state.type==2){
                    state.Room.OnlineNum!=Msg.OnlineNum?state.Room.OnlineNum=Msg.OnlineNum:false;
                    let i;
                    for(i in Msg.Content){
                        state.Room[i]!=Msg.Content[i]?state.Room[i]=Msg.Content[i]:false;
                    }
                }
                else if(state.type==3){
                    for(let a=0;a<state.ManyGame.length;a++){
                        if(state.ManyGame[a].GType==Msg.GT&&state.ManyGame[a].TId==Msg.GTableId){
                            state.ManyGame[a].OnlineNum!=Msg.OnlineNum?state.ManyGame[a].OnlineNum=Msg.OnlineNum:false;
                            let ab;
                            for(ab in Msg.Content){
                                state.ManyGame[a][ab]!=Msg.Content[ab]?state.ManyGame[a][ab]=Msg.Content[ab]:false;
                            }
                        }
                    }
                }
                window.AudioObj.Play(['stopbet'])
            },
            // 紧急作废处理
            clearGmae(state,Msg){
                state.Init.UserCentent.Balance!=Msg.Content.Balance?state.Init.UserCentent.Balance=Msg.Content.Balance:false;
                if(state.type==2){
                    state.Room.CurrState=7;
                    state.Room.OnlineNum!=Msg.OnlineNum?state.Room.OnlineNum=Msg.OnlineNum:false;
                    state.Room.CurrJu!=Msg.Content.CurrJu?state.Room.CurrJu=Msg.Content.CurrJu:false;
                    state.Room.CurrXue!=Msg.Content.CurrXue?state.Room.CurrXue=Msg.Content.CurrXue:false;
                    state.Room.RestHops=0;
                    state.Room.ChipsSum="0/0,0/0,0/0,0/0,0/0";
                    state.Room.BetHistory="0,0,0,0,0,0,0,0";
                    state.Room.G_Result="";
                    state.Room.PaiMian="";
                    state.Room.WinLose=0;
                }
                else if(state.type==3){
                    for(let i=0;i<state.ManyGame.length;i++){
                        if(state.ManyGame[i].TId==Msg.GTableId&&state.ManyGame[i].GType==Msg.GT){
                            state.ManyGame[i].CurrState=7;
                            state.ManyGame[i].OnlineNum!=Msg.OnlineNum?state.ManyGame[i].OnlineNum=Msg.OnlineNum:false;
                            state.ManyGame[i].CurrJu!=Msg.Content.CurrJu?state.ManyGame[i].CurrJu=Msg.Content.CurrJu:false;
                            state.ManyGame[i].CurrXue!=Msg.Content.CurrXue?state.ManyGame[i].CurrXue=Msg.Content.CurrXue:false;
                            state.ManyGame[i].RestHops=0;
                            state.ManyGame[i].ChipsSum="0/0,0/0,0/0,0/0,0/0";
                            state.ManyGame[i].BetHistory="0,0,0,0,0,0,0,0";
                            state.ManyGame[i].G_Result="";
                            state.ManyGame[i].PaiMian="";
                            state.ManyGame[i].WinLose=0;
                        }
                    }
                }
            },
            // 进入多台处理方法
            setManyGame(state,Msg){
                state.type=3;
                state.Init.UserCentent.Balance!=Msg.Content.Balance?state.Init.UserCentent.Balance=Msg.Content.Balance:false;
                // 预防与进入多台与开牌冲突
                for(let i=0;i<Msg.Content.RoomList.length;i++){
                            Msg.Content.RoomList[i].G_Result="";
                            Msg.Content.RoomList[i].PaiMian="";
                            Msg.Content.RoomList[i].WinLose=0;
                }
                state.ManyGame=Msg.Content.RoomList;
                for(let i=Msg.Content.RoomList.length-1;i>-1;i--){
                            // console.log(Msg.Content.RoomList[i].TId);
                            window.ioSocket.emit('subscribe',"RoomBroadCastAfterTZ"+Msg.Content.RoomList[i].TId);
                }
            },
            // 多台时间减少
            setManyTime(state,index){
                state.ManyGame[index].RestHops>0?state.ManyGame[index].RestHops--:false;
            },
            //初始化多台数据，防止数据乱不更新
            initManyGame(state){
                state.ManyGame=[
                    {
                        BetHistory:"0,0,0,0,0,0,0,0",
                        BetMode:0,
                        ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                        CurrJu:0,
                        CurrState:0,
                        CurrXue:0,
                        DjsTime:null,
                        GType:0,
                        G_Result:"",
                        HallNumber:1,
                        LuDan:"",
                        MoneyMax:0,
                        MoneyMin:0,
                        PaiMian:"",
                        RestHops:null,
                        RoomName:"",
                        TId:0,
                        VideoUrl:"",
                        WinLose:0
                    },
                    {
                        BetHistory:"0,0,0,0,0,0,0,0",
                        BetMode:0,
                        ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                        CurrJu:0,
                        CurrState:0,
                        CurrXue:0,
                        DjsTime:null,
                        GType:0,
                        G_Result:"",
                        HallNumber:1,
                        LuDan:"",
                        MoneyMax:0,
                        MoneyMin:0,
                        PaiMian:"",
                        RestHops:null,
                        RoomName:"",
                        TId:0,
                        VideoUrl:"",
                        WinLose:0
                    },
                    {
                        BetHistory:"0,0,0,0,0,0,0,0",
                        BetMode:0,
                        ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                        CurrJu:0,
                        CurrState:0,
                        CurrXue:0,
                        DjsTime:null,
                        GType:0,
                        G_Result:"",
                        HallNumber:1,
                        LuDan:"",
                        MoneyMax:0,
                        MoneyMin:0,
                        PaiMian:"",
                        RestHops:null,
                        RoomName:"",
                        TId:0,
                        VideoUrl:"",
                        WinLose:0
                    },
                    {
                        BetHistory:"0,0,0,0,0,0,0,0",
                        BetMode:0,
                        ChipsSum:"0/0,0/0,0/0,0/0,0/0",
                        CurrJu:0,
                        CurrState:0,
                        CurrXue:0,
                        DjsTime:null,
                        GType:0,
                        G_Result:"",
                        HallNumber:1,
                        LuDan:"",
                        MoneyMax:0,
                        MoneyMin:0,
                        PaiMian:"",
                        RestHops:null,
                        RoomName:"",
                        TId:0,
                        VideoUrl:"",
                        WinLose:0
                    },
        
                ]
            },
            // 公共数据区被处理完毕清空
            ClearPublicWarehouse(state,x){
                if(!x.index){//房间
                    state.publicWarehouse[x]=false;	
                    return false;			
                }else if(x.index==3){
                    state.publicWarehouse.ManyGame.splice(x.data,1,{RoomBetInfo:false,RoomLoserBet:false})
                }
            },
            // 返回数据发送错误
            errorPs(state,x){
                switch(x.ErrorCode){
                    case 10001:
                        this.commit("changeTips",{data:"当前账号已锁定，请与客服联系,将自动退出游戏", Out:true});
                        break;
                    case 10002:
                        this.commit("changeTips",{data:"当前账号本次登陆请求被拒绝，请稍候重试,将自动退出游戏",Out: true});
                        break;
                    case 10003:
                        this.commit("changeTips",{data:"服务器繁忙，请稍候登录,将自动退出游戏", Out:true});
                        break;
                    case 10004:
                        this.commit("changeTips",{data:"非法操作，请稍候登录,将自动退出游戏",Out: true});
                        break;
                    case 10005:
                        this.commit("changeTips",{data:"服务器繁忙，请重试"});
                        break;
                    case 10006:
                        this.commit("changeTips",{data:"数据格式有误，请重试"});
                        break;
                    case 10007:
                        this.commit("changeTips",{data:"数据包格式有误，请重试"});
                        break;
                    case 10008:
                        this.commit("changeTips",{data:"用户名有误，请重新登录,将自动退出游戏", Out:true});
                        break;
                    case 10019:
                        this.commit("changeTips",{data:"超出限红范围，本次押注无效"});
                        this.commit("setBetInLoser",x);
                        break;
                    case 10020:
                        this.commit("changeTips",{data:"押注超时，请下局再押"});
                        this.commit("setBetInLoser",x);
                        break;
                    case 10021:
                        this.commit("changeTips",{data:"账户余额不足"});
                        break;
                    case 10022:
                        this.commit("changeTips",{data:"登录超时，请重新登录,将自动退出游戏", Out:true});
                        break;
                    case 10023:
                        this.commit("changeTips",{data:"数据行为传入有误，请重新登录,将自动退出游戏",Out: true});
                        break;
                    case 10024:
                        this.commit("changeTips",{data:"登录已超时，请重新登录,将自动退出游戏",Out: true});
                        break;
                    case 10025:
                        this.commit("changeTips",{data:"投注异常"});
                        break;
                    case 10026:
                        this.commit("changeTips",{data:"当前账号禁止投注，请与客服联系,将自动退出游戏", Out:true});
                        break;
                    case 10027:
                        this.commit("changeTips",{data:"投注失败，请重试"});
                        break;
                }
                this.commit("changeLoading",false);
            },
            // 处理视频播放显示加载屏障
            setVideoStatus(state,msg){
                state.VideoStatus.splice(msg.index,1,msg.data);
            }
        },
        actions:{
            // 登录游戏
            starWs(context){
                let index=0;
                let _this=this;
                let onloade=function(){
                    //加载成功，促进加载条
                    window.loader.setLoad("加载重要文件",10);
                    
                    // 初始化配置
                    // 心跳包
                    let head='{"ActType":11,"ConnectionId":"","GT":"","UId":"' + UserId + '","ErrorCode":10000,"RandCode":"'+ RandCode + '"}'
                    // 绑定数据处理函数
                    function mesg(id,msg){
                        if(id!=UserId&&msg.ErrorCode){
                            alert("请不要非法登陆！！！，正在退出");
                            sessionStorage.clear();
                            window.location.href="./index.html";
                            return false;
                        }
                        msg=JSON.parse(msg)//数据转换成JSON
                        if(msg.ErrorCode!=10000&&msg.ErrorCode){
                            this.commit("errorPs",msg);//进行数据错误判断
                            return false;
                        }
                        console.log(msg);
                        switch(msg.ActType){//执行数据交互任务
                            case 1://登陆
                                window.loader.setLoad("正在加载重要事件",7);//****/登陆成功
                                // console.log("登录")
                                // console.log(msg)
                                this.commit("setInit",msg);
                                this.commit("changeLoading",false);
                            break;
                            case 2://投注
                                // console.log("投注数据")
                                // console.log(msg)
                                this.commit("getBetInfo",msg);
                                this.commit("changeLoading",false);
                            break;
                            case 3://大厅
                                // console.log('大厅数据');
                                // console.log(msg)
                                this.commit("goGameHall",msg);
                                this.commit("changeLoading",false);
                            break;
                            case 5://房间
                                // console.log('房间数据');
                                // console.log(msg)
                                this.commit("setRoomInit",msg);
                                this.commit("changeLoading",false);
                            break;
                            case 9://开奖
                                // console.log("开奖")
                                // console.log(msg);
                                this.commit("openRule",msg);
                            break;
                                // 下注比例信息更新
                            case 19:
                                // console.log("彩池数据")
                                // console.log(msg)
                                this.commit("setUserAll",msg);
                                this.commit("changeTableRoom",msg)
                            break;
                            case 17://开局返回
                                this.commit("openNewGame",msg);
                            break;
                            // 停止下注
                            case 23:
                                this.commit("setStop",msg);
                            break;
                            case 22://断开链接
                                this.commit("changeTips",{data:"当前账号已在其他地方登陆,即将自动退出游戏",Out: true})
                            break;
                            // 本局作废
                            case 24:
                                this.commit("clearGmae",msg);
                            break;
                            case 28:
                                this.commit("RoomStore/setPublicTable",msg);
                            break;
                            case 30://多台
                                // console.log('多台：')
                                // console.log(msg)
                                this.commit("setManyGame",msg)
                                this.commit("changeLoading",false);
                            break;
                            default:
                                if(this.state.type==1){
                                    this.commit("changeTableRoom",msg)
                                }else{
                                    // console.log('公共房间数据')
                                    //公共列表数据处理
                                    this.commit("RoomStore/setRoomPublic",msg);
                                }
                                if(this.state.type==3){
                                        this.commit('openNewGame',msg);
                                }
                                
                        }
                                
                            }
                    // 发送信息回调函数
                    function sendFun(Msg){
                        let a= JSON.parse(Msg);
                        if(a.ActType==2){
                            window.TimeOut?clearTimeout(window.TimeOut):false;
                            window.TimeOut=setTimeout(()=>{
                                this.commit("changeLoading",{id:"下注中",GTableId:a.GTableId,GType:a.GT});
                            },500)
                        }else if(a.ActType==11||a.ActType==28||a.ActType==12){
                            // 心跳包不执行任何动作
                        }
                        else{
                            window.TimeOut?clearTimeout(window.TimeOut):false;
                            window.TimeOut=setTimeout(()=>{
                                this.commit("changeLoading",true);
                                window.interrupt=setTimeout(()=>{
                                    context.commit("errorPs",{ErrorCode:10022});
                                },20000)
                            },500)	
                        }
                    }
                    // 发送错误回调
                    function errro(msg){
                        console.log(msg)
                    }
                    // 确认网络中断触发
                    function onclose(){
                        if(sessionStorage.getItem('RandCode')){
                            console.log("无法链接到服务器");
                            setTimeout(()=>{
                                this.commit("changeTips",{data:"与服务器链接断开，请重新登陆", Out:true})
                            },1000)							
                        }
                    }
                    // 网络出现延迟高触发
                    function Slow(){
                        console.log("当前网络较慢")
                    }
                    // 网络出现重新链接触发
                    function res(){
                        alert("网络断开，重新链接")
                    }
                    // 网络重新链接成功
                    function resYes(){
                        alert("网络重新链接成功")
                    }
                    // 首次链接成功回调
                    function Success(){
                        // 发送登陆验证数据
                        // console.log("登陆链接成功")
                        this.commit("changeLoading",true);
                        window.userSocket.uSend('{"ActType":1,"ClientIp":"","ConnectionId":"","Content":{"UId":"' + UserId + '"},"ErrorCode":10000,"RandCode":"' + RandCode + '","UId":"' + UserId + '"}');
                        window.loader.setLoad("正在加载重要事件",0)
                    }
                    // 开启链接
                    window.userSocket= new SignalSocket("http://"+wsNum[index]+"/signalr",UserId,head,25000,mesg.bind(context),errro,sendFun.bind(context),onclose.bind(context),Slow,res,resYes,Success.bind(context));
                    // 开启socket.io
                    window.ioSocket=io('http://192.168.1.181:18188');
                    // window.ioSocket=io('http://118.193.156.222:6088');
                    // 首次链接成功
                    window.ioSocket.on('open',()=>{
                        window.ioSocket.emit('subscribe',"RoomListInRoom11");						
                        for(let j = 29; j >= 1; j--){
                            window.ioSocket.emit('subscribe',"RoomBroadCastAfterTZ"+j);
                        }
                    })
                    window.ioSocket.on('msgReceived',(msg)=>{
                        mesg.call(context,UserId,msg);
                    })
                    window.ioSocket.on('join',(msg)=>{
                        // console.log('进入房间：'+msg)
                        context.commit('chanegRadisnName',msg);
                        // console.log('当前订阅对象属性：')
                        // console.log(context.getters.ListStatusName)
                    })
                }
                function setUrl(x){
                    // 动态加载HUBS代理文件
                    let HubJS= document.createElement("script");
                    
                    HubJS.src="http://"+wsNum[x]+"/signalr/hubs";
                    // 插入到文档
                    document.getElementsByTagName("head")[0].appendChild(HubJS);
    
                    HubJS.onload=onloade.bind(_this);
    
                    // HubJS.src="http://"+wsNum[x]+"/signalr/hubs";
    
                    HubJS.onerror=function(){//加载动态JS文件失败，服务器关闭
                        index++;
                        if(index<wsNum.length){
                            setUrl(index);
                        }else{
                            context.commit("changeTips",{data:"加载系统文件失败，请稍后重试", Out:true});
                        }
                    }
                }
                setUrl(0);
            },
            setVideoStatus(context,msg){
                context.commit("setVideoStatus",msg);
            }
        },
        modules:{
            RoomStore,
            log,
            User
        }
    
})
